var vaDay = "";
var vaMonth = "";
var vaYear = "";
$(document).ready(function() {
	getEmployee();
	getReceiveCode();
	
	checkDisableAndEnableSaveButton(vaStatusSave);
	if ( vaStatusIsUpdate =='success'){
		$('#myModalMessageConfirm').modal('hide');
		showTab(vaPageSetting);
	}
});

//$(function () {
//	getEmployee();
//	getReceiveCode();
//	
//	checkDisableAndEnableSaveButton(vaStatusSave);
//	if ( vaStatusIsUpdate =='success'){
//		$('#myModalMessageConfirm').modal('hide');
//		showTab(vaPageSetting);
//	}
//	
//	
//});

function checkButtonClick(button) {	
	if(button == "btn_save"){
		document.getElementById("btn_save").value = "yes";
	}
}

function checkDisableAndEnableSaveButton(vaStatusSave){
	if ( vaStatusSave == '' || vaStatusSave =='btn_enable'){
		document.getElementById("div_save_step1").innerHTML = "<div class='form-detail' style='text-align:center; margin-top:-50px'> " +
		" <button type='button'  class='btn btn-primary' data-toggle='modal' data-target='#myModalMessageConfirm' " +
		" id='btnSave_step1' name='btnSave_step1'><i class='fa fa-floppy-o'></i> บันทึก</button></div>";
	} else if ( vaStatusSave == 'btn_disable'){
		document.getElementById("div_save_step1").innerHTML = "<div class='form-detail' style='text-align:center; margin-top:-50px'> " +
		" <button type='button' disabled  class='btn btn-primary' data-toggle='modal' data-target='#myModalMessageConfirm' " +
		" id='btnSave_step1' name='btnSave_step1'><i class='fa fa-floppy-o'></i> บันทึก</button></div>";
	}
}

function showTab(vaPageSetting) {
	// show tab
	if (vaPageSetting == "1") {
		// default setting
	} else if (vaPageSetting == "2") {
		// set to display tab 2
		$('#manage_getmoney').removeClass('active');
		$('#summary_getmoney').addClass('active');
		$('.form-step').hide();
		$('#form-2').show();
	} else if (vaPageSetting == "3") {
		// set to display tab summary
		$('#manage_getmoney').removeClass('active');
		$('#report_getmoney').addClass('active');
		$('.form-step').hide();
		$('#form-3').show();
	}
}

function getEmployee() {

	$.getJSON('CS54001Srvl', {
		process_type: "getEmployee"
	}, function(result) {

		var options = '';
		options += '<option value="">-- เลือกพนักงาน  --</option>';
		for (var i = 0; i < result.values.length; i++) {

			options += '<option value="' + result.values[i] + '">' + result.messages[i] +  '</option>';
		}
		vaDay = result.vaDay;
		vaMonth = result.vaMonth;
		vaYear = result.vaYear;
		$("#InputEmployee").html(options); 
	});
}

function getReceiveCode() {

	$.getJSON('CS54001Srvl', {
		process_type: "getReceive"
	}, function(result) {

		var options = '';
		options += '<option value="">-- ทุกรหัส --</option>';
		for (var i = 0; i < result.values.length; i++) {

			options += '<option value="' + result.values[i] + '">' + result.messages[i] +  '</option>';
		}
		$("#InputReceiveCode").html(options); 
	});
}

function printReport() {
	
	var vaDate = vaDay+"/"+vaMonth+"/"+vaYear;
	var vaReportType = $("#inputReportType").val();
	var vaInputEmployee = $("#InputEmployee").val();
	var vaInputReceiveCode = $("#InputReceiveCode").val();
	var vnUserId = $("#userId").val();
	var vnSiteId = $("#siteId").val();
	
	if(vaDate.trim().length == 0 || vaReportType.trim().length == 0 || vaInputEmployee.trim().length == 0 
			|| vaInputReceiveCode.trim().length == 0)
	{
		$("#modal_content").html("กรุณากรอกข้อมูลให้ครบถ้วน");
		$("#warningModal").modal();
		return;
	}else{
		var url = reporturl;
		
		if(vaReportType == "before_receive"){
			url = url+"CS56008R.jsp";
			vaInputReceiveCode = "&receiveCode="+vaInputReceiveCode;
		} else if(vaReportType == "water_per_day"){
			url = url+"CS56009R.jsp";
			vaInputReceiveCode = "";
		} else if(vaReportType == "waste_pre_day"){
			url = url+"CS56010R.jsp";
			vaInputReceiveCode = "";
		} else if(vaReportType == "wastewater_pre_day"){
			url = url+"CS56011R.jsp";
			vaInputReceiveCode = "";
		} else if(vaReportType == "other_per_day"){
			url = url+"CS56012R.jsp";
			vaInputReceiveCode = "";
		} else if(vaReportType == "revenue_per_day"){
			url = url+"CS56013R.jsp";
			vaInputReceiveCode = "";
		}

		url = url+"?siteId="+vnSiteId+"&userId="+vnUserId+"&fromDate="+vaDate+"&toDate="+vaDate+"&receiveEmployee="+vaInputEmployee+""+vaInputReceiveCode;
		window.open(url);
	}
	
}